package com.goods.business.service.impl;

import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Consumer;
import com.goods.common.model.business.Supplier;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author jiaoyanlin
 * @Date 2021/11/10 17:00
 * @Description SupplierServiceImpl
 */
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;
    //物资来源
    @Override
    public PageVO<SupplierVO> findSupplierList(Integer pageNum, Integer pageSize, Map<String, Object> map) {
        PageHelper.startPage(pageNum,pageSize);
        Object name = map.get("name");
        Object address = map.get("address");
        Object contact = map.get("contact");
        Example example =new Example(Consumer.class);
        Example.Criteria criteria = example.createCriteria();
        if (null!=name){
            String s = Convert.toStr(name);
            criteria.andLike("name","%"+s+"%");
        }

        if (null!=address){
            String address1=Convert.toStr(address);
            criteria.andLike("address","%"+address1+"%");
        }
        if (null!=contact){
            String contact1=Convert.toStr(contact);
            criteria.andLike("contact","%"+contact1+"%");
        }
        List<Supplier> suppliers = supplierMapper.selectByExample(example);
        List<SupplierVO> supplierVOS=new ArrayList<>();
        if (!CollectionUtils.isEmpty(suppliers)){
            for (Supplier supplier : suppliers) {
                SupplierVO supplierVO = new SupplierVO();
                BeanUtils.copyProperties(supplier,supplierVO);
                supplierVOS.add(supplierVO);
            }
            List<SupplierVO> page = ListPageUtils.page(supplierVOS, pageSize, pageNum);
            PageInfo info = new PageInfo<>(page);
            return new PageVO<SupplierVO>(info.getTotal(),supplierVOS);
        }

        return null;

    }

    @Override
    public Boolean addSupplierVO(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplier,supplierVO);

        try {
            supplierMapper.insert(supplier);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public SupplierVO editSupplierVO(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier,supplierVO);
        return supplierVO;
    }

    @Override
    public Boolean updateSupplierVO(SupplierVO supplierVO, Long id) {
        Supplier supplier = new Supplier();
        supplierVO.setId(id);
        BeanUtils.copyProperties(supplierVO,supplier);
        try {
            supplierMapper.updateByPrimaryKey(supplier);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public void deleteSupplierVO(Long id) {
        supplierMapper.deleteByPrimaryKey(id);
    }
}
